<template>
  <view class="viewport">
    <scroll-view enable-back-to-top scroll-y class="scroll">
      <uni-section title="编辑" type="line">
        <view class="example">
          <uni-forms ref="baseForm" :modelValue="form">
            <uni-forms-item label="【全局】隐藏金额" label-width="500">
              <uni-data-checkbox v-model="form.IsAmount" :localdata="checkdata" mode="default" />
            </uni-forms-item>
            <uni-forms-item label="【明细】默认查询账户" label-width="500">
              <picker
                @change="pickerAccountTypeChange"
                mode="selector"
                :range="accountTypeNames"
                range-key="AccountTypeName"
              >
                <text class="account">{{ form.DefaultAccountNo }}</text></picker
              >
            </uni-forms-item>
            <uni-forms-item label="【记账】默认记账账户" label-width="500">
              <PickerAccountPanel
                v-if="ready"
                @change="pickerDefaultNoteAccountChange"
                :userAccounts="userAccounts"
                :curUserAccount="DefaultNoteAccount"
              ></PickerAccountPanel>
            </uni-forms-item>
            <uni-forms-item label="【转账】默认转出账户" label-width="500">
              <PickerAccountPanel
                v-if="ready"
                @change="pickerDefaultOutAccountChange"
                :userAccounts="userAccounts"
                :curUserAccount="DefaultOutAccount"
              ></PickerAccountPanel>
            </uni-forms-item>
            <uni-forms-item label="【转账】默认转入账户" label-width="500">
              <PickerAccountPanel
                v-if="ready"
                @change="pickerDefaultInAccountChange"
                :userAccounts="userAccounts"
                :curUserAccount="DefaultInAccount"
              ></PickerAccountPanel>
            </uni-forms-item>
            <uni-forms-item label="每月预算">
              <uni-easyinput v-model="formPlan.PlanAmountPreMonth" placeholder="请输入每月预算" />
            </uni-forms-item>
            <uni-forms-item label="应急总金额">
              <uni-easyinput v-model="formPlan.YingJiAmount" placeholder="请输入应急总金额" />
            </uni-forms-item>
            <uni-forms-item label="应急警告线">
              <uni-easyinput v-model="formPlan.YingJiWarn" placeholder="请输入应急警告线" />
            </uni-forms-item>
            <uni-forms-item label="应急危险线">
              <uni-easyinput v-model="formPlan.YingJiError" placeholder="请输入应急危险线" />
            </uni-forms-item>
          </uni-forms>
        </view>
      </uni-section>
      <button class="button btn" @click="ok">确认</button>
      <button class="button btn" @click="back">取消</button>
    </scroll-view>
  </view>
</template>
<script setup lang="ts">
import PickerAccountPanel from '@/pages/NoteAcount/PickerAccountPanel.vue'
import {
  GetAccountTypeNameAPI,
  GetUserAccountAPI,
  GetUserSettingAPI,
  UpdateUserSettingAPI,
} from '@/services/detail'
import { GetYuSuanAPI, UpdateYuSuanAPI } from '@/services/login'
import { useMemberStore } from '@/stores'
import type { YuSuan } from '@/types/count'
import type { AccountTypeNameResponse, UserAccount, UserSetting } from '@/types/detail'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import { useRoute } from 'vue-router'

//============================以下变量========================
const route = useRoute()
// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()
// 获取会员信息
const ready = ref(false)
const memberStore = useMemberStore()
const form = ref<UserSetting>({
  UserName: '',
  IsAmount: false,
  IsSearchSelf: false,
  DefaultAccountNo: '',
  DefaultOutAccountNo: '',
  DefaultInAccountNo: '',
  DefaultNoteAccountNo: '',
  MyOpacit: 0,
  DetailOpacit: 0,
  CountOpacit: 0,
  NoteAccoutOpacit: 0,
  ZichanOpacit: 0,
  TagSelf: '',
  SoftStyle: '',
})
const formPlan = ref<YuSuan>({
  ID: 0,
  UserGroup: '',
  PlanAmountPreMonth: 0,
  YingJiAmount: 0,
  YingJiWarn: 0,
  YingJiError: 0,
})

const curAccountTypeName = ref<AccountTypeNameResponse>({
  AccountTypeName: 'ALL',
})
const checkdata = [
  { value: true, text: '是' },
  { value: false, text: '否' },
]
const DefaultNoteAccount = ref<UserAccount>({
  AccountNo: '',
  UserName: '',
  AccountTypeName: '',
  AccountCard: '',
  Display: '',
  Remark: '',
})
const accountTypeNames = ref<AccountTypeNameResponse[]>()
const DefaultAccount = ref<UserAccount>({
  AccountNo: '',
  UserName: '',
  AccountTypeName: '',
  AccountCard: '',
  Display: '',
  Remark: '',
})
const DefaultInAccount = ref<UserAccount>({
  AccountNo: '',
  UserName: '',
  AccountTypeName: '',
  AccountCard: '',
  Display: '',
  Remark: '',
})
const DefaultOutAccount = ref<UserAccount>({
  AccountNo: '',
  UserName: '',
  AccountTypeName: '',
  AccountCard: '',
  Display: '',
  Remark: '',
})
const userAccounts = ref<UserAccount[]>([])
//============================以下函数========================
//加载初始化
onLoad(async (option) => {
  await InitUserAccount()
  await InitAccountTypeName()
  await InitYuSuan()
  await reFresh()
  curAccountTypeName.value.AccountTypeName = form.value.DefaultNoteAccountNo
  DefaultNoteAccount.value = userAccounts.value.find(
    (o) => o.AccountNo == form.value.DefaultNoteAccountNo,
  )!
  DefaultAccount.value = userAccounts.value.find((o) => o.AccountNo == form.value.DefaultAccountNo)!
  DefaultInAccount.value = userAccounts.value.find(
    (o) => o.AccountNo == form.value.DefaultInAccountNo,
  )!
  DefaultOutAccount.value = userAccounts.value.find(
    (o) => o.AccountNo == form.value.DefaultOutAccountNo,
  )!
  ready.value = true
})

//刷新事件
const reFresh = async () => {
  if (memberStore.profile) {
    const res = await GetUserSettingAPI({
      UserName: memberStore.profile.UserName,
    })
    form.value = res.result
  } else {
    uni.showToast({ icon: 'none', title: '请先登录' })
    setTimeout(() => {
      // 页面跳转
      uni.switchTab({ url: '/pages/my/my' })
    }, 500)
  }
}
//初始化账户列表
const InitUserAccount = async () => {
  if (memberStore.profile) {
    const res = await GetUserAccountAPI({
      UserName: memberStore.profile.UserName,
      Auth: memberStore.profile.Token,
      UserGroup: memberStore.profile.UserGroup,
    })
    userAccounts.value = res.result
  }
}
//初始化预算
const InitYuSuan = async () => {
  if (memberStore.profile) {
    const res = await GetYuSuanAPI({
      Auth: memberStore.profile.Token,
      UserGroup: memberStore.profile.UserGroup,
    })
    formPlan.value = res.result
  }
}
//返回
const back = () => {
  uni.switchTab({ url: '/pages/my/my' })
}
//确认
const ok = async () => {
  await UpdateUserSettingAPI(form.value)
  await UpdateYuSuanAPI({
    Auth: memberStore!.profile!.Token!,
    UserGroup: formPlan.value.UserGroup,
    PlanAmountPreMonth: formPlan.value.PlanAmountPreMonth,
    YingJiAmount: formPlan.value.YingJiAmount,
    YingJiWarn: formPlan.value.YingJiWarn,
    YingJiError: formPlan.value.YingJiError,
    ID: formPlan.value.ID,
  })
  // 成功提示
  uni.showToast({ icon: 'success', title: '保存成功' })
  setTimeout(() => {
    // 页面跳转
    uni.switchTab({ url: '/pages/my/my' })
  }, 500)
}
const pickerDefaultAccountChange = (e: any) => {
  DefaultAccount.value = e
  form.value.DefaultAccountNo = DefaultAccount.value.AccountNo
}
const pickerDefaultNoteAccountChange = (e: any) => {
  DefaultNoteAccount.value = e
  form.value.DefaultNoteAccountNo = DefaultNoteAccount.value.AccountNo
}
const pickerDefaultInAccountChange = (e: any) => {
  DefaultInAccount.value = e
  form.value.DefaultInAccountNo = DefaultInAccount.value.AccountNo
}
const pickerDefaultOutAccountChange = (e: any) => {
  DefaultOutAccount.value = e
  form.value.DefaultOutAccountNo = DefaultOutAccount.value.AccountNo
}
//初始化账户类型列表
const InitAccountTypeName = async () => {
  if (memberStore.profile) {
    const res = await GetAccountTypeNameAPI({
      Auth: memberStore.profile.Token,
      UserGroup: memberStore.profile.UserGroup,
    })
    res.result.push({
      AccountTypeName: 'ALL',
    })
    accountTypeNames.value = res.result
  }
}
//选择账户类型
const pickerAccountTypeChange = (e: any) => {
  const index = e.detail.value as number
  curAccountTypeName.value = accountTypeNames.value![index]
  form.value.DefaultAccountNo = curAccountTypeName.value.AccountTypeName
}
</script>
<style lang="scss">
@import './styles/setting.scss';
</style>
